#include "stdafx.h"
#include "Resource.h"

#include "MainFrame.h"
#include "CXComp.h"
#include "Claxa.h"

#include <MyViews/Resource.h>
#include <MyControls/Resource.h>
#include ".\mainframe.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// MainFrame

BEGIN_MESSAGE_MAP(MainFrame, MainFrame::Base)
	ON_WM_CREATE()
END_MESSAGE_MAP()

My::ToolButtonInfo tools[] = {
	{ID_TOOL_PAN, "TOOL_HAND"},
	{ID_TOOL_ZOOM, "TOOL_ZOOM"},
	{0, 0},
	{ID_TOOL_BRUSH, "TOOL_BRUSH"},
	{ID_TOOL_FILL, "TOOL_FILL"},
	{0, 0},
/*	{ID_TOOL_SELECT, "TOOL_ARROW"},
	{0, 0},
	{ID_TOOL_BRUSH, "TOOL_BRUSH"},
	{ID_TOOL_FILL, "TOOL_FILL"},
	{ID_SEGMENT_IMPORTMASK, "IMPORT_MASK"},
	{ID_SEGMENT_EXPORTMASK, "EXPORT_MASK"},
	{0, 0},
*/	{ID_EDIT_UNDO, "TOOL_UNDO"},
	{ID_EDIT_REDO, "TOOL_REDO"}
};

MainFrame::MainFrame(CXComp & comp) : 
	Base(comp),
	m_Comp(comp),
	m_View(comp),
	m_Timeline(comp),
	m_TimelinePanel(* this, m_Timeline),
	m_Tools(My::ToolBar::VERTICAL),
	m_ToolPanel(* this, m_Tools, 4),
	m_PEditPanel(* this, m_PEditor),
	m_TimeControlsPanel(* this, m_TimeControls),
	m_DisplayPanel(* this)
	//My::Frame attaches itself to listening comp's events; no need to do it again
{
	m_LargeFont.CreateFont(-11, 0, 0, 0, 400, FALSE, FALSE, 0,  DEFAULT_CHARSET, 0, 0, 0, 0, "Tahoma");
	UpdateFont();

	m_Timeline.GetTimeRuler().SetFont(&m_LargeFont);
	m_TimelinePanel.SetCaption("Timeline");
	m_TimelinePanel.SetAlignment(My::Panel::ALIGN_CLIENT);
	GetBottomBar().AddPanel(&m_TimelinePanel);

	m_Tools.SetButtonSize(CSize(21, 21));
	m_Tools.SetButtons(tools, sizeof(tools) / sizeof(My::ToolButtonInfo));
	m_ToolPanel.SetCaption("Tools");
	m_ToolPanel.SetAlignment(My::Panel::ALIGN_CLIENT);
	GetToolBar().AddPanel(&m_ToolPanel);

	m_DisplayPanel.SetCaption("Display");
	m_DisplayPanel.SetAlignment(My::Panel::ALIGN_TOP);
	GetRightBar().AddPanel(&m_DisplayPanel);

	m_TimeControls.SetSequence(&m_Comp);
	m_TimeControls.SetSequenceView(&m_View);
	m_TimeControlsPanel.SetCaption("Time Controls");
	m_TimeControlsPanel.SetAlignment(My::Panel::ALIGN_TOP);
	GetRightBar().AddPanel(&m_TimeControlsPanel);

	m_PEditor.SetFont(&m_LargeFont);
	m_PEditPanel.SetCaption("Properties");
	m_PEditPanel.SetAlignment(My::Panel::ALIGN_CLIENT);
	GetRightBar().AddPanel(&m_PEditPanel);
}

MainFrame::~MainFrame()
{
}

int MainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (Base::OnCreate(lpCreateStruct) == -1)
		return -1;
	CMenu * pMenu = GetMenu()->GetSubMenu(0)->GetSubMenu(13)->GetSubMenu(2);
	theApp.UpdateLanguageMenu(pMenu);

	// create a view to occupy the client area of the frame
	if (!m_View.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
		CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
	{
		TRACE0("Failed to create view window\n");
		return -1;
	}

	RecalcLayout();

	return 0;
}

void MainFrame::ProcessEvent(My::Event & e)
{
	if (e.Is<My::Doc::SelectionChange>()) {
		OnSelectionChanged();
	} 
	else if (e.Is<My::Sequence::OpenEvent>()) {
		OnSelectionChanged();
		GetDisplayPanel().SetView(&m_View);
	} 
	else if (e.Is<My::Sequence::CloseEvent>()) {
		m_PEditor.Close();
		GetDisplayPanel().SetView(0);
	}
	else if (e.Is<My::CommandEventBase>()) {
		My::CommandEventBase & cmd = e.As<My::CommandEventBase>();
		m_Timeline.ProcessEvent(cmd);
		if (cmd.m_Handled)
			return;
	}
	Base::ProcessEvent(e);
}

void MainFrame::OnSelectionChanged()
{
	if (m_Comp.GetSegments().GetActiveSegmentPtr() != 0)
		m_PEditor.Open(m_Comp.GetSegments().GetActiveSegmentPtr());
	else
		m_PEditor.Open(&m_Comp);
}

BOOL MainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	if (m_Comp.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;
	return Frame::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
